package com.bm.main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.bm.model.Message;
import com.bm.util.Fungsi;
import com.bm.util.MessagePrinter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Mirza
 */
public class IsiOutbox extends Thread {

    private Message message;
    private MISMS p;

    public IsiOutbox(Message msg, MISMS parent) throws Exception {
        message = msg;
        p = parent;
    }

    @Override
    public void run() {
        FileWriter fw = null;
        try {
            String namaFile = message.getId_message() + ".txt";
            String file = "";
            String file2 = "";
            boolean isSuccess = false;            
            String status = "";
            if (message.getReceiver().equals("") || message.getReceiver().length() < 10 || message.getContent().equals("")) {
                status = "-2";
            } else {
                file = p.dir_outbuff + namaFile;
                if (message.getId_modul().equals("DEFAULT")){
                    file2 = p.dir_out + namaFile;
                } else {
                    file2 = p.dir_out + message.getId_modul() +"/" + namaFile;
                }                 
                status = "-1";
            }

            final Object[] data = {message.getId_message(), status};
            final SocketChecker sc = new SocketChecker();
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    sc.getSocketClient().execute("MISMS.updateMessageOutbox", data);
                }
            });
            t.start();



            if (!file.equalsIgnoreCase("")) {
                String content = message.getContent();
                File f = new File(file);
                fw = new FileWriter(f);
                fw.append("To: " + message.getReceiver() + "\r\n");
                fw.append("Queue: " + message.getId_modul()+ "\r\n");
                fw.append("Modem: " + message.getId_modul()+ "\r\n");
                fw.append("\r\n");
                fw.append(content);
                fw.flush();
                fw.close();

                if (!file2.isEmpty()) {
                    f = new File(file);
                    File f2 = new File(file2);
                    if (f.renameTo(f2)) {
                        isSuccess = true;
                    }
                }
                
                if (isSuccess) {
                    p.tampilBerita(namaFile + " berhasil dibuat di directory outgoing.");
                } else {
                    p.tampilBerita(namaFile + " gagal dibuat di directory outgoing.");
                }
            }
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } finally {
            MessagePrinter.printLog("[" + Fungsi.getDetailTime() + "] " + message.getId_message() + " finish isi outbox");
            try {
                if (fw != null) {
                    fw.close();
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
}
